package com.hnxxxy.rewardmgr.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.PageHelper;
import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.common.annotation.LogAnno;
import com.hnxxxy.common.tools.AppFileUtils;
import com.hnxxxy.common.tools.Help;
import com.hnxxxy.common.tools.TableObj;
import com.hnxxxy.rewardmgr.bean.ExcelBean;
import com.hnxxxy.rewardmgr.bean.NumberConfig;
import com.hnxxxy.rewardmgr.bean.RewardLevel;
//import com.hnxxxy.rewardmgr.utils.MessageUtils;
import com.hnxxxy.rewardmgr.utils.UploadUtils;
import com.hnxxxy.rewardmgr.utils.Variable;
//import com.hnxxxy.sysmgr.Test;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.Rows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.hnxxxy.rewardmgr.service.RewardService;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.*;

@RequestMapping("reward")
@Slf4j
@Controller
public class RewardController {
    @Autowired
    private RewardService rewardServiceImpl;

    /*//所有学生数据
    private static List<NumberConfig> numberConfigListAllOf0 = null;
    //所有老师数据
    private static List<NumberConfig> numberConfigListAllOf1 = null;
    private static int size = 0;
    private static int isSMS = 0;*/


    @RequestMapping(value = "uploadNumberConifg", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRtnEntity uploadNumberConifg(@RequestParam(value = "excelFile") MultipartFile excelFile) {
        AjaxRtnEntity ajaxRtnEntity = new AjaxRtnEntity();
        String oldName = excelFile.getOriginalFilename();
        //4、构造文件夹
        assert oldName != null;
        File dirFile = UploadUtils.getImgDirFile("null");

        //File[] files = dirFile.listFiles();
        File newFile = new File(dirFile.getAbsolutePath() + File.separator + oldName);
        try {
            excelFile.transferTo(newFile);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }

        List<ExcelBean> list = new ArrayList<>();
        EasyExcel.read(newFile, ExcelBean.class, new AnalysisEventListener<ExcelBean>() {
            @Override
            public void invoke(ExcelBean excelBean, AnalysisContext analysisContext) {
                list.add(excelBean);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                int flag = rewardServiceImpl.batchInsertNumberConfig(list);
                if (flag > 0) {
                    ajaxRtnEntity.setCode(1);
                    ajaxRtnEntity.setInfo("写入成功!");
                } else {
                    ajaxRtnEntity.setCode(0);
                    ajaxRtnEntity.setInfo("写入失败!");
                }
            }
        }).sheet().doRead();

        return ajaxRtnEntity;
    }

    @RequestMapping("levelConifg")
    public String levelConifg() {
        return "reward/levelConifg";
    }


    @RequestMapping("numberConfig")
    public String numberConfig() {
        return "reward/numberConfig";
    }


    @RequestMapping("queryInfo")
    public String queryInfo() {
        return "reward/rewardInfo";
    }


    @RequestMapping("loadRewardLevel")
    @ResponseBody
    @LogAnno(description = "抽奖配置查询")
    public TableObj loadRewardLevel(TableObj tableObj) {
        PageHelper.startPage(tableObj.getCurr(), tableObj.getNums());
        List<RewardLevel> list = rewardServiceImpl.loadRewardLevel();
        return Help.getPageTableRtn(tableObj, list);
    }

    @RequestMapping("numberConifg")
    @ResponseBody
    @LogAnno(description = "抽奖人数配置")
    public TableObj numberConfig(TableObj tableObj) {
        PageHelper.startPage(tableObj.getCurr(), tableObj.getNums());
        List<NumberConfig> list = rewardServiceImpl.numberConfig();
        return Help.getPageTableRtn(tableObj, list);

    }

    @RequestMapping("deleteRewardLevel")
    @ResponseBody
    public AjaxRtnEntity deleteRewardLevel(int id) {
        AjaxRtnEntity ajaxRtnEntity = new AjaxRtnEntity();
        int flag = rewardServiceImpl.deleteRewardLevel(id);
        if (flag > 0) {
            ajaxRtnEntity.setCode(1);
            ajaxRtnEntity.setInfo("删除成功");
        } else {
            ajaxRtnEntity.setCode(0);
            ajaxRtnEntity.setInfo("成功失败");
        }
        return ajaxRtnEntity;
    }


    @RequestMapping("deleteNumberConfig")
    @ResponseBody
    public AjaxRtnEntity deleteNumberConfig(String id) {
        AjaxRtnEntity ajaxRtnEntity = new AjaxRtnEntity();
        int flag = rewardServiceImpl.deleteNumberConfig(id);
        if (flag > 0) {
            ajaxRtnEntity.setCode(1);
            ajaxRtnEntity.setInfo("删除成功");
        } else {
            ajaxRtnEntity.setCode(0);
            ajaxRtnEntity.setInfo("删除失败");
        }
        return ajaxRtnEntity;
    }

    @RequestMapping("updateNumberConfig")
    @LogAnno(description = "抽奖人信息变更")
    @ResponseBody
    public AjaxRtnEntity updateNumberConfig(NumberConfig numberConfig) {
        AjaxRtnEntity ajaxRtnEntity = new AjaxRtnEntity();
        int flag = rewardServiceImpl.updateNumberConfig(numberConfig);
        if (flag > 0) {
            ajaxRtnEntity.setCode(1);
            ajaxRtnEntity.setInfo("删除成功");
        } else {
            ajaxRtnEntity.setCode(0);
            ajaxRtnEntity.setInfo("成功失败");
        }
        return ajaxRtnEntity;
    }

    @RequestMapping("cleanNumberConfig")
    @ResponseBody
    @LogAnno(description = "清空参赛人信息")
    public AjaxRtnEntity cleanNumberConfig() {
        int row = this.rewardServiceImpl.cleanNumberConfig();
        return new AjaxRtnEntity(row, "清空成功！", null);
    }


    @RequestMapping("insertNumberConfig")
    @LogAnno(description = "抽奖人信息新增")
    @ResponseBody
    public AjaxRtnEntity insertNumberConfig(NumberConfig numberConfig) {

        AjaxRtnEntity ajaxRtnEntity = new AjaxRtnEntity();
        int flag = rewardServiceImpl.insertNumberConfig(numberConfig);
        if (flag > 0) {
            ajaxRtnEntity.setCode(1);
            ajaxRtnEntity.setInfo("删除成功");
        } else {
            ajaxRtnEntity.setCode(0);
            ajaxRtnEntity.setInfo("删除失败");
        }
        return ajaxRtnEntity;
    }

    @RequestMapping("batchDeleteLevel")
    @ResponseBody
    @LogAnno(description = "批量删除抽奖配置")
    public AjaxRtnEntity batchDeleteLevel(Integer[] ids) {

        List<Integer> list = new ArrayList<>(Arrays.asList(ids));
        //  int i = this.rewardServiceImpl.batchDeleteLevel(list);
        return new AjaxRtnEntity(this.rewardServiceImpl.batchDeleteLevel(list), "删除成功", null);
    }


    @RequestMapping("updateRewardLevel")
    @ResponseBody
    @LogAnno(description = "更新抽奖等级信息")
    public AjaxRtnEntity updateRewardLevel(RewardLevel rwd) {
        AjaxRtnEntity ajaxRtnEntity = new AjaxRtnEntity();
        if (!(null != rwd.getImg_url() && rwd.getImg_url().equals("images/meinv.jpg"))) {
            if (rwd.getImg_url().endsWith("_temp")) {
                String newName = AppFileUtils.rename(rwd.getImg_url());
                rwd.setImg_url(newName);
                String img_url = this.rewardServiceImpl.levelConfigById(rwd.getId()).getImg_url();
                try {
                    AppFileUtils.removeFileByPath(img_url);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        int flag = rewardServiceImpl.updateRewardLevel(rwd);
        if (flag > 0) {
            ajaxRtnEntity.setCode(1);
            ajaxRtnEntity.setInfo("修改成功");
        } else {
            ajaxRtnEntity.setCode(0);
            ajaxRtnEntity.setInfo("修改失败");
        }
        return ajaxRtnEntity;

    }


    @RequestMapping("insertRewardLevel")
    @LogAnno(description = "配置抽奖等级")
    @ResponseBody
    public AjaxRtnEntity insertRewardLevel(RewardLevel rwd) {
        AjaxRtnEntity ajaxRtnEntity = new AjaxRtnEntity();
        if (rwd.getImg_url() != null && rwd.getImg_url().endsWith("_temp")) {
            System.out.println(rwd.getImg_url());
            String newName = AppFileUtils.rename(rwd.getImg_url());
            System.out.println("IMG_PATH + newName========>"  + newName);
            rwd.setImg_url( newName);
        }
        int flag = rewardServiceImpl.insertRewardLevel(rwd);
        if (flag > 0) {
            ajaxRtnEntity.setCode(1);
            ajaxRtnEntity.setInfo("添加成功");
        } else {
            ajaxRtnEntity.setCode(0);
            ajaxRtnEntity.setInfo("添加失败");
        }
        return ajaxRtnEntity;
    }

    @RequestMapping("showReward")
    @LogAnno(description = "查询抽奖信息")
    @ResponseBody
    public TableObj showReward(TableObj tableObj) {
        PageHelper.startPage(tableObj.getCurr(), tableObj.getNums());
        List<NumberConfig> list = rewardServiceImpl.showReward();
        return Help.getPageTableRtn(tableObj, list);
    }

    @RequestMapping("showReward1")
    @LogAnno(description = "查询得奖人信息")
    @ResponseBody
    public TableObj showReward1(TableObj tableObj, String name, String numbers, String rwd_state) {
        PageHelper.startPage(tableObj.getCurr(), tableObj.getNums());
        List<NumberConfig> list = rewardServiceImpl.loadNumberConfig(name, numbers, rwd_state);
        return Help.getPageTableRtn(tableObj, list);
    }


    @GetMapping(value = "/getList")
    @LogAnno(description = "初始化，拿到所有数据")
    @ResponseBody
    public Map<String, Object> startLuckDraw() {
        return rewardServiceImpl.getAll();
    }

    @GetMapping(value = "/getRatio/{id}")
    @LogAnno(description = "抽奖方法")
    @ResponseBody
    public Map<String, List<NumberConfig>> ratio(@PathVariable("id") int id) {
        return rewardServiceImpl.getRatioNumber(id);
    }

}